package com.cjlgb.design.upms.controller;

import com.cjlgb.design.common.core.bean.ResultBean;
import com.cjlgb.design.upms.api.entity.SysRole;
import com.cjlgb.design.upms.service.SysRoleMenuService;
import com.cjlgb.design.upms.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * @author WFT
 * @date 2019/8/16
 * description: 后台角色控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/sys/role")
public class SysRoleController {

    private final SysRoleService roleService;
    private final SysRoleMenuService roleMenuService;

    /**
     * 查询角色列表
     * @param params 查询参数
     * @return IPage<IPage>
     */
    @GetMapping(value = "/list")
    @PreAuthorize(value = "@pms.hasPermission('permission.role.query','permission.user.add')")
    public ResultBean<List<SysRole>> pagingQuery(SysRole params){
        return ResultBean.ok(this.roleService.list(params));
    }

    /**
     * 修改角色信息
     * @param role 角色信息
     * @return ResultBean
     */
    @PutMapping
    @PreAuthorize(value = "@pms.hasPermission('permission.role.edit')")
    public ResultBean editRole(@RequestBody SysRole role){
        Assert.notNull(role.getId(),"角色Id不能为空");
        Assert.hasText(role.getName(),"角色名称不能为空");
        return this.roleService.editRole(role);
    }

    /**
     * 添加角色
     * @param role 角色信息
     * @return ResultBean
     */
    @PostMapping
    @PreAuthorize(value = "@pms.hasPermission('permission.role.add')")
    public ResultBean addRole(@RequestBody SysRole role){
        Assert.hasText(role.getName(),"角色名称不为空");
        return this.roleService.createRole(role);
    }

    /**
     * 批量删除角色
     * @param idList 角色Id列表
     * @return ResultBean
     */
    @PostMapping(value = "/batch/delete")
    @PreAuthorize(value = "@pms.hasPermission('permission.role.delete')")
    public ResultBean batchDelete(@RequestBody List<Long> idList){
        Assert.notEmpty(idList,"角色Id不能为空");
        return this.roleService.batchDelete(idList);
    }

    /**
     * 获取角色的详细信息
     * @param id 角色Id
     * @return ResultBean<SysRole>
     */
    @GetMapping(value = "/{roleId}")
    public ResultBean<SysRole> getRoleInfo(@PathVariable(value = "roleId") Long id){
        SysRole role = this.roleService.getById(id);
        Assert.notNull(role,"角色Id不存在");
        return ResultBean.ok(role);
    }

    /**
     * 获取角色的权限Id列表
     * @param id 角色Id
     * @return ResultBean<Collection<Long>>
     */
    @GetMapping(value = "/authorities/{roleId}")
    @PreAuthorize(value = "@pms.hasPermission('permission.role.authorities')")
    public ResultBean<Collection<Long>> getAuthorities(@PathVariable(value = "roleId") Long id){
        return ResultBean.ok(this.roleMenuService.selectRoleAuthorities(id,3));
    }

    /**
     * 更改角色的权限
     * @param role 角色信息
     * @return ResultBean
     */
    @PutMapping(value = "/authorities")
    @PreAuthorize(value = "@pms.hasPermission('permission.role.authorities')")
    public ResultBean changeAuthorities(@RequestBody SysRole role){
        Assert.notNull(role.getId(),"角色Id不能为空");
        Assert.notNull(role.getAuthorities(),"角色权限Id列表不能为空");
        return this.roleService.changeAuthorities(role);
    }

}
